function [] = plot_activity(ROIs,bv,np,template,scan_info)


% generate the on/off shading
shading.x(1)=0;
shading.y(1)=0;
for ind=2:4*scan_info.total_frames/(scan_info.on+scan_info.off)
    if rem(ind,2)==1
        shading.x(ind)=shading.x(ind-1);
    elseif rem(ind,4)==0
        shading.x(ind)=shading.x(ind-1)+scan_info.on;
    else
        shading.x(ind)=shading.x(ind-1)+scan_info.off;
    end
    if rem(ind-1,4)>1 
        shading.y(ind)=1;
    else
        shading.y(ind)=0;
    end
end


% plot template with cells overlayed
max_cont=mean(template(:))+2*std(template(:));
min_cont=mean(template(:))-2*std(template(:));

hf = figure('menubar','none');
ha = axes('position',[0 0 1 1]);

tmp=(template-min_cont)/(max_cont-min_cont);
tmp(tmp>1)=1;
tmp(tmp<0)=0;

t_mask=zeros(size(template));
bv_mask=zeros(size(template));

for ind=1:length(ROIs)
    t_mask(ROIs(ind).indices)=1;
    bv_mask(bv.indices)=1;
end

overlay(:,:,1)=tmp;
overlay(:,:,2)=bwperim(t_mask);
overlay(:,:,3)=bwperim(bv_mask);
hi=imagesc(overlay);
hold on

for ind=1:length(ROIs)
    [txt_x,txt_y]=ind2sub([400 400],min(ROIs(ind).indices));
    text(txt_y,txt_x-10,num2str(ind),'color','w','fontweight','bold')
end


hf2 = figure('visible','off','color','k','menubar','none');
ha2 = axes('position',[0 0.07 1 0.93],'ytick',[],'color','k','xcolor','w');

hf3 = figure('visible','off','color','k','menubar','none');
ha3 = axes('color','k','xcolor','w','ycolor','w');

hf4 = figure('visible','off','color','k','menubar','none');
ha4 = axes('color','k','xcolor','w','ycolor','w');

set(hi,'buttondownfcn',{@plot_activity_bdf,template,ROIs,np,bv,scan_info,shading,hf2,hf3,hf4,ha});
box off
%axis off

    



function [] = plot_activity_bdf(hf,e,template,ROIs,np,bv,scan_info,shading,hf2,hf3,hf4,ha)

cp=get(ha,'currentpoint');
sel_ind = sub2ind(size(template),round(cp(3)),round(cp(1)));

plot_shift=0.2;


for ind=1:length(ROIs)
    if sum(sel_ind == ROIs(ind).indices) == 1
        sel_roi = ind;
    end
end

if exist('sel_roi')
    figure(hf2);
    set(hf2,'visible','on');
    hold on
    cla
    sel_data=calc_dFF(ROIs(sel_roi).activity);
    np_act=calc_dFF(np.activity);
    bv_act=calc_dFF(bv.activity);
    
    bv_corr_act=calc_dFF(ROIs(sel_roi).activity - ((bv.activity) - mean(bv.activity)));
    np_corr_act=calc_dFF(ROIs(sel_roi).activity - 1*((np.activity) - mean(np.activity)));
    
    area(shading.x,3*shading.y*(max(sel_data)-min(sel_data))+min(sel_data),min(sel_data),'edgecolor','none','facecolor',[1 1 1]*0.1)
    yl=ylim;
    
    
    plot(bv_act,'color',[0.5 0.5 1])
    plot(np_act,'y')
    plot(bv_corr_act+plot_shift,'color', [0.5 0.5 1]);
    plot(np_corr_act+plot_shift,'y');
    plot(sel_data+2*plot_shift,'w')
       
    plot([10 10],[0 0.1],'w')

    seq_length = scan_info.seq * (scan_info.on+scan_info.off);
    
    for ind=1:scan_info.repetitions
        plot([1 1]*(ind*scan_info.total_frames/scan_info.repetitions+1),yl,'w','linewidth',2);
    end
%     for ind=1:scan_info.total_frames/seq_length
%         plot([1 1]*(ind*seq_length),yl,':w','linewidth',1)
%     end
 
    text(0,0.9,[' Cell nr. ' num2str(sel_roi)],'color','w','units','normalized','fontsize',15,'fontweight','bold')
    axis tight
    
    
    figure(hf3);
    set(hf3,'visible','on');
    hold on
    cla
    area(shading.x(1:4),0.2*shading.y(1:4),'edgecolor','none','facecolor',[1 1 1]*0.1)
    
    sel_data_by_trial=reshape(sel_data,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off));
    bv_corr_act_by_trial=reshape(bv_corr_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off));
    np_corr_act_by_trial=reshape(np_corr_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off));
    
    
    plot(mean(sel_data_by_trial')+0.5*plot_shift,'color', 'w')
    plot(mean(bv_corr_act_by_trial')+0.5*plot_shift,'color', [0.5 0.5 1])
    plot(mean(np_corr_act_by_trial')+0.5*plot_shift,'color','y')
    
    plot(mean(reshape(bv_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off))'),'color', [0.5 0.5 1])
    plot(mean(reshape(np_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off))'),'color','y')
    
    sel_data_trial_sig=signrank(mean(sel_data_by_trial(1:5,:)),mean(sel_data_by_trial(6:15,:)));
    bv_corr_trial_sig=signrank(mean(bv_corr_act_by_trial(1:5,:)),mean(bv_corr_act_by_trial(6:15,:)));
    np_corr_trial_sig=signrank(mean(np_corr_act_by_trial(1:5,:)),mean(np_corr_act_by_trial(6:15,:)));
    
    text(0.1, 1, ['p = ' num2str(sel_data_trial_sig)],'color','w','units','normalized')
    text(0.4, 1, ['p = ' num2str(bv_corr_trial_sig)],'color',[0.5 0.5 1],'units','normalized')
    text(0.7, 1, ['p = ' num2str(np_corr_trial_sig)],'color','y','units','normalized')
    
    
    figure(hf4);
    set(hf4,'visible','on');
    hold on
    cla
    area(shading.x,0.2*shading.y,'edgecolor','none','facecolor',[1 1 1]*0.1)
    
    sel_data_by_reps=reshape(sel_data',scan_info.repetitions,scan_info.total_frames/scan_info.repetitions);
    bv_corr_act_by_reps=reshape(bv_corr_act',scan_info.total_frames/scan_info.repetitions,scan_info.repetitions);
    np_corr_act_by_reps=reshape(np_corr_act',scan_info.total_frames/scan_info.repetitions,scan_info.repetitions);
    
    
    plot(sel_data_by_reps'+0.5*plot_shift,'color', 'w')
%     plot(bv_corr_act_by_reps'+0.5*plot_shift,'color', [0.5 0.5 1])
%     plot(np_corr_act_by_reps+0.5*plot_shift,'color','y')
    xlim([0 scan_info.total_frames/scan_info.repetitions])
    
%     plot(mean(reshape(bv_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off))'),'color', [0.5 0.5 1])
%     plot(mean(reshape(np_act,scan_info.on+scan_info.off,scan_info.total_frames/(scan_info.on+scan_info.off))'),'color','y')
%     

    
else
    disp('No cell selected')
end


